package com.gitee.simons.oauth.config;

import com.gitee.simons.common.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author simon jiang
 * @time 2020/7/8 17:18
 */
@Component
@Slf4j
public class MyPasswordEncoder implements PasswordEncoder {

    /**
     * 加密
     *
     * @param charSequence 需要加密的密码
     * @return 加密后的密码
     */
    @Override
    public String encode(CharSequence charSequence) {
        return MD5Util.getMD5Str(charSequence.toString());
    }

    /**
     * 密码对比
     *
     * @param charSequence 传输的密码，
     * @param s            数据库中记录的密码
     * @return 是否匹配
     */
    @Override
    public boolean matches(CharSequence charSequence, String s) {
        log.info("charSequence:{},s:{}", charSequence, s);
        return s.equals(MD5Util.getMD5Str(charSequence.toString()));
    }
}
